package com.cqc.zhbjcqc.fragment.frag2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.cqc.zhbjcqc.R;
import com.cqc.zhbjcqc.activity.MainActivity;
import com.cqc.zhbjcqc.adapter.FragNewsAdapter;
import com.cqc.zhbjcqc.bean.Frag2Bean;
import com.cqc.zhbjcqc.fragment.BaseFragment;
import com.cqc.zhbjcqc.fragment.frag2.fragnews.FragNewsItem;
import com.cqc.zhbjcqc.view.PagerSlidingTab;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cui on 2016/9/27.
 * 新闻
 */

public class FragNews extends BaseFragment {
    private View rootView;
    private PagerSlidingTab pagerTab;
    private ViewPager viewPager;

    private List<String> titleList = new ArrayList<>();
    private List<String> urlList = new ArrayList<>();
    private List<FragNewsItem> fragsList = new ArrayList<>();
    private Frag2Bean.DataBean dataBean;
    public FragNews(Frag2Bean.DataBean dataBean) {
        super();
        this.dataBean = dataBean;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frag_news, container, false);
        findViews();
        initViews();
        return rootView;
    }

    private void initViews() {
        initData();

        FragNewsAdapter adapter = new FragNewsAdapter(((MainActivity) context).getSupportFragmentManager(), fragsList,titleList);
        viewPager.setAdapter(adapter);
        pagerTab.setViewPager(viewPager);

        //修复bug：只有第一个tab的时候才可以打开slidingMenu
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0){
                    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                } else {
                    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        titleList.clear();
        urlList.clear();
        fragsList.clear();
        for (int i = 0; i < dataBean.getChildren().size(); i++) {
            titleList.add(dataBean.getChildren().get(i).getTitle());
            urlList.add(dataBean.getChildren().get(i).getUrl());
            FragNewsItem fragNewsItem = new FragNewsItem(dataBean.getChildren().get(i).getUrl());
            fragsList.add(fragNewsItem);
        }
    }

    private void findViews() {
        pagerTab = (PagerSlidingTab) rootView.findViewById(R.id.pagerSlidingTab);
        viewPager = (ViewPager) rootView.findViewById(R.id.viewPager_frag_news);
    }
}
